import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Random;


// Project: 

/******************************************************************************
 * <p>
 * File: enemyControl.java
 * </p>
 * @author  Tadhg O'Sullivan, rogue, Goncalo Bastos
 * @author <a href="mailto:tadhg.osullivan@ucdconnect.ie">tadhg.osullivan@ucdconnect.ie</a>
 * @version 1.0, 2007
 * @created 2-Mar-07 8:01:40 PM
 * @revised 
 * Copyright 2007 Tadhg O'Sullivan
 * <p>
 * Description: A class for controlling enemy sprite movement
 * </p>
 *  
 *  This file is part of Retribution.
 *
 *  Retribution is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Retribution is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Retribution; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *   
 *****************************************************************************/

public class enemyControl {
	/**
	 * 
	 * @param e An enemy sprite
	 * @param mt An array filled with maptiles
	 * @param h the hero sprite
	 * @param el the rest of the enemy sprites
	 */
	GameSounds s= new GameSounds();
	public void moveEnemy(LinkedList<enemy> el,hero h, enemy e, mapTile[][] mt, int mapHeigth){
			Interaction inter = new Interaction();
			if(!e.getAttackState()){
		if((h.getX()==e.getX() && h.getY()==e.getY()+(8*(mapHeigth/200)))||(h.getX()==e.getX() && h.getY()==e.getY()-(8*(mapHeigth/200)))||(h.getX()==e.getX()-(8*(mapHeigth/200)) && h.getY()==e.getY())||(h.getX()==e.getX()+(8*(mapHeigth/200)) && h.getY()==e.getY())){
			try {
				s.SwordHit();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			inter.enemyAttack(h, e);			
		}
			}
		Random generator = new Random();
	    int randomIndex = generator.nextInt( 4 );
	    if(!e.getAttackState()){
		if(randomIndex == 0){
			// check for map collision
			if((mt[(e.getY() + 32)/ 32][e.getX() / 32].collisionState == true)||(h.getX()==e.getX() && h.getY()==e.getY()+(8*(mapHeigth/200)))||((((h.getY())/ (8*(mapHeigth/200)))) == 24)){
				moveEnemy(el,h,e,mt,mapHeigth);
			}else{
			e.setX(e.getX());
			e.setY(e.getY() + 32);
			e.setMoved();
			}
		}
		else if(randomIndex == 1){
			// check for map collision
			if((mt[(e.getY() - 32) / 32][e.getX() / 32].collisionState == true)||(h.getX()==e.getX() && h.getY()==e.getY()-(8*(mapHeigth/200)))||((((h.getY())/ (8*(mapHeigth/200)))) == 0)){
				moveEnemy(el,h,e,mt,mapHeigth);
			}else{
			e.setX(e.getX());
			e.setY(e.getY() - 32);
			e.setMoved();
			}
		}
		else if(randomIndex == 2){
			// check for map collision
			if((mt[e.getY() / 32][(e.getX() - 32)/ 32].collisionState == true)||(h.getX()==e.getX()-(8*(mapHeigth/200)) && h.getY()==e.getY())||((((h.getX())/ (8*(mapHeigth/200)))) == 0)){
				moveEnemy(el,h,e,mt,mapHeigth);
			}else{
			e.setX(e.getX() - 32);
			e.setY(e.getY());
			e.setMoved();
			}
		}
		else if(randomIndex == 3){
			// check for map collision
			if((mt[e.getY() / 32][(e.getX() + 32)/ 32].collisionState == true)||(h.getX()==e.getX()+(8*(mapHeigth/200)) && h.getY()==e.getY())||((((h.getX())/ (8*(mapHeigth/200)))) == 39)){
				moveEnemy(el,h,e,mt,mapHeigth);
			}else{
			e.setX(e.getX() + 32);
			e.setY(e.getY());
			e.setMoved();
			}
		}
		}
	}
}
